{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2518ff53-f8a6-4ae2-a6f7-32e012d05e62",
   "metadata": {},
   "source": [
    "# Python流程与执行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "781047e7-f385-4ed2-81d5-c5b358046ed7",
   "metadata": {},
   "source": [
    "## 编程语言的共性问题\n",
    "*变量和变量名\n",
    "*变量类型：int,float,str,bool,complex\n",
    "*运算符：+ - * / > = < >= <= == != % += -+ *= /= **\n",
    "*程序执行的流程\n",
    " **顺序执行\n",
    "   程序一行一行地执行\n",
    "   \n",
    " **循环执行\n",
    "  for <循环变量> in range(循环次数):\n",
    "      <循环代码>\n",
    " **分支\n",
    "  if <条件>：\n",
    "     <条件1代码>\n",
    "  elif <条件2>："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77c44c02-777a-4730-b832-50d43b5ef6ee",
   "metadata": {},
   "source": [
    "# 顺序执行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "94a89799-d2bb-42d1-bc9b-c9c26210e786",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "261c1bfd04934fc98155ec8d456465c9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(height=350, width=500)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import ipyturtle3 as turtle\n",
    "from ipyturtle3 import hold_canvas\n",
    "myCanvas=turtle.Canvas(width=500,height=350)\n",
    "display(myCanvas)\n",
    "myTS=turtle.TurtleScreen(myCanvas)\n",
    "myTS.clear()#背景清除\n",
    "myTS.bgcolor(\"lightgreen\")#背景颜色\n",
    "bob=turtle.Turtle(myTS)\n",
    "bob.shape(\"turtle\")\n",
    "myTS.delay(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9179fd7b-9092-47f2-91a0-90fd966b0d2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "step=0.5\n",
    "for i in range(3):\n",
    "    for _ in range(360):\n",
    "        bob.forward(step)\n",
    "        bob.left(1)\n",
    "    step+=0.5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "254af1ee-bf0e-40dc-8c52-6815c26bf963",
   "metadata": {},
   "source": [
    "# 龟兔赛跑"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "a84745ee-8828-4161-aa1a-b43d7acff288",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8035d93ecb6b469dad3f2bef2edbc8b4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(height=350, width=500)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "兔子赢得了比赛\n"
     ]
    }
   ],
   "source": [
    "import ipyturtle3 as turtle\n",
    "from ipyturtle3 import hold_canvas\n",
    "myCanvas=turtle.Canvas(width=500,height=350)\n",
    "display(myCanvas)\n",
    "myTS=turtle.TurtleScreen(myCanvas)\n",
    "myTS.clear()#背景清除\n",
    "myTS.bgcolor(\"black\")#背景颜色\n",
    "tt=turtle.Turtle(myTS,isHolonomic=True)\n",
    "tt.shape(\"turtle\")\n",
    "tt.color(\"green\")\n",
    "rb=turtle.Turtle(myTS,isHolonomic=True)\n",
    "rb.shape(\"square\")\n",
    "rb.color(\"pink\")\n",
    "myTS.delay(10)\n",
    "tt.penup()\n",
    "rb.penup()\n",
    "tt.goto(-200,100)\n",
    "rb.goto(-200,-100)\n",
    "tt.pendown()\n",
    "rb.pendown()\n",
    "import random\n",
    "distance_tt=0\n",
    "distance_rb=0\n",
    "x=0\n",
    "while distance_tt<200 or distance_rb<200:\n",
    "    colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow']\n",
    "    with(hold_canvas(myCanvas)):\n",
    "        tt.pencolor(colors[x%6])\n",
    "        rb.pencolor(colors[x%6])\n",
    "    if distance_rb<30:\n",
    "        distance_rb+=10\n",
    "        distance_tt+=1\n",
    "        rb.forward(10)\n",
    "        tt.forward(1)\n",
    "        x+=1\n",
    "    else:\n",
    "        num=random.randint(1,100)\n",
    "        if num<80:\n",
    "            distance_tt+=1\n",
    "            distance_rb+=0\n",
    "            rb.forward(0)\n",
    "            tt.forward(1)\n",
    "            x+=1\n",
    "        elif num<95:\n",
    "            distance_tt+=1\n",
    "            distance_rb+=7\n",
    "            rb.forward(7)\n",
    "            tt.forward(1)\n",
    "            x+=1\n",
    "        else:\n",
    "            distance_tt+=1\n",
    "            distance_rb+=10\n",
    "            rb.forward(10)\n",
    "            tt.forward(1)\n",
    "            x+=1\n",
    "if distance_tt<distance_rb:\n",
    "    print(\"兔子赢得了比赛\")\n",
    "elif distance_tt==distance_rb:\n",
    "    print(\"比赛平了，没有输家\")\n",
    "else:\n",
    "    print(\"乌龟赢得了比赛\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea05d014-56bc-4dc6-855d-a493f0041ad3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
